Ext.onReady(function() {
	Ext.apply(Ext.form.VTypes, {
	    numbers:  function(v) {
	        return /^[0-9]+$/.test(v);
	    },
	    numbersText: 'Must be a numeric IP address',
	    numbersMask: /[0-9]/i
	});

	var store = new Ext.data.Store({// 配置分组数据集
		autoLoad : {
			params : {
				start : 0,
				limit : 5
			}
		},// 默认加载参数：start=0，limit=5
		reader : new Ext.data.JsonReader({
					totalProperty : 'totalProperty',
					root : "infoList"
				}, Ext.data.Record.create([// 对应JSON中的属性名称
						{
									name : 'apartmentname'
								}, {
									name : 'apartmentfloornum',
									type : 'float'
								}, {
									name : 'apartmenttotalrooms',
									type : 'float'
								}, {
									name : 'apartmentmgrname'
								}, {
									name : 'apartmenttelephone'
								}])),
		proxy : new Ext.data.HttpProxy({// URL:struts.xml中配置该action，相对地址没写对
			url : 'http://5i407.gicp.net:8080/DMS/aptmgr/getAptJSON.action'
		})
	});
	var menu = new Ext.menu.Menu({
        id: 'mainMenu',
        style: {
            overflow: 'visible'     // For the Combo popup
        },
        items: [                  // A Field in a Menu
            {
                text: '(双击编辑所选行)'
            }
        ]
    });
 var tb = new Ext.Toolbar();
 tb.add({
            text:'(双击编辑所选行)'
        });

	var gridPanel = new Ext.grid.GridPanel({
				id : 'aptGrid',
				stripeRows : true,
				height : 350,
				width : 600,
				title : '公寓列表',
				store : store,
				loadMask : {
					msg : '正在加载数据，请稍侯……'
				},
				tbar: tb,
				columns : [{
							id : 'aptName',
							header : '公寓名称',
							width : 75,
							sortable : true,
							dataIndex : 'apartmentname'
						}, {
							header : '楼层总数',
							width : 75,
							sortable : true,
							dataIndex : 'apartmentfloornum'
						}, {
							header : '宿舍总数',
							width : 75,
							sortable : true,
							dataIndex : 'apartmenttotalrooms'
						}, {
							header : '管理员',
							width : 75,
							sortable : true,
							dataIndex : 'apartmentmgrname'
						}, {
							id : 'aptPhone',
							header : '公寓电话',
							width : 100,
							sortable : true,
							dataIndex : 'apartmenttelephone'
						}],
				//autoExpandColumn : 'aptPhone',
				bbar : new Ext.PagingToolbar({
							pageSize : 5,
							store : store,
							displayInfo : true,
							displayMsg : '显示第 {0}条 至 {1}条 共 {2}条',
							emptyMsg : "没有满足条件的记录"
						})
			});
	gridPanel.addListener('rowdblclick', cellclick);
	

	function cellclick(grid, rowIndex, e) {
		var record = grid.getStore().getAt(rowIndex); //Get the Record
		// var fieldName = grid.getColumnModel().getDataIndex(columnIndex);
		// //Get field name
		// var data = record.get(fieldName);
		// Ext.MessageBox.alert('show','asdf'+rowIndex);

		var formPanel = new Ext.FormPanel({
	        labelWidth: 75, // label settings here cascade unless overridden
	        //url:'save-form.php',
	        frame:true,
	        bodyStyle:'padding:5px 5px 0',
	        //width: 350,
	        defaults: {width: 230},
	        defaultType: 'textfield',
	
	        items: [{
	                fieldLabel: '公寓名称',
	                value:record.get('apartmentname'),
	                name: 'apartmentname'
	            },{
	                fieldLabel: '楼层总数',
	                value:record.get('apartmentfloornum'),
	                name: 'apartmentfloornum',
                	vtype:'numbers'

	            },{
	                fieldLabel: '宿舍总数',
	                value:record.get('apartmenttotalrooms'),
	                name: 'apartmenttotalrooms'
	            }, {
	                fieldLabel: '管理员',
	                value:record.get('apartmentmgrname'),
	                name: 'apartmentmgrname'
	            }, {
	            	fieldLabel: '公寓电话',
	            	value:record.get('apartmenttelephone'),
	                name: 'apartmenttelephone'
	            }
	        ]
	    });

		var win = new Ext.Window({
					title : '公寓信息',
					layout : 'fit',
					width : 400,
					height : 300,
					closeAction : 'hide',
					plain : true,
					modal : true,
					items : [formPanel],
					buttons : [{
								text : '修改',
								handler : function() {
									Ext.MessageBox.alert('ERROR','TODO……');
								}
							}, {
								text : '取消',
								handler : function() {
									win.hide();
								}
							}]
				});
		win.show();
		// Ext.MessageBox.show(win);
	}

	var centerPanel = new Ext.TabPanel({
				id : 'tabs',
				frame : false,
				region : 'center',
				activeTab : 0,
				items : [gridPanel, {
							id : 'glPanel',
							title : '宿舍管理',
							autoScroll : true,
							html : 'TODO……'
						}, {
							id : 'bjPanel',
							title : '标记房间',
							autoScroll : true,
							html : 'TODO……'
						}, {
							id : 'pbPanel',
							title : '宿舍评比',
							autoScroll : true,
							html : 'TODO……'
						}]
			});
	// 布局
	var viewport = new Ext.Viewport({
				layout : 'border',
				items : [centerPanel]
			});

	Ext.get('loading').remove();
});